import { registerAs } from '@nestjs/config';
import {TypeOrmModuleOptions} from "@nestjs/typeorm";

interface AppConfig {
  name: string;
  port: string;
  prefix: string;
  database: TypeOrmModuleOptions;
  jwt: {
    secret: string;
    expiresIn: string;
  };
  redis: {
    host: string;
    port: number;
    password: string;
    db: number;
  };
  log: {
    level: string;
    on: boolean;
  };
  swagger: {
    path: string;
    enable: boolean;
  };
  wechat: {
    appId: string;
    appSecret: string;
  };
}

export default registerAs('app', (): AppConfig => ({
  // App
  name: process.env.APP_NAME || '',
  port: process.env.SERVER_PORT || '9000' ,
  prefix: process.env.API_PREFIX || 'api',

  // Database
  database: {
    type: process.env.DB_TYPE,
    host: process.env.DB_HOST,
    port: process.env.DB_PORT || 3306,
    database: process.env.DB_DATABASE,
    username: process.env.DB_USERNAME,
    password: process.env.DB_PASSWORD,
    synchronize: process.env.DB_SYNC === 'true',
    logging: process.env.NODE_ENV === 'development',
  } as TypeOrmModuleOptions,

  // JWT
  jwt: {
    secret: process.env.JWT_SECRET,
    expiresIn: process.env.JWT_EXPIRES_IN,
  } as any,

  // Redis
  redis: {
    host: process.env.REDIS_HOST,
    port: process.env.REDIS_PORT,
    password: process.env.REDIS_PASSWORD,
    db: process.env.REDIS_DB,
  } as any,

  // Log
  log: {
    level: process.env.LOG_LEVEL || 'debug',
    on: process.env.LOG_ON === 'true',
  } as any,

  // Swagger
  swagger: {
    path: process.env.SWAGGER_PATH || 'docs',
    enable: process.env.SWAGGER_ENABLE === 'true',
  } as any,

  // WeChat
  wechat: {
    appId: process.env.WECHAT_APP_ID,
    appSecret: process.env.WECHAT_APP_SECRET,
  } as any,
}));
